<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019/1/3
 * Time: 下午5:04
 */

namespace Api\Controller;


use Api\Service\CollectionsService;

class CollectionsController extends LoginBaseController
{

    //获取收藏列表
    public function collectionsList(){
        $where['waoo_collections.uid']=$this->uid;
        $shareInfo=CollectionsService::S()->queryCollections($where);
        foreach ($shareInfo as $k=>$v){
            if(empty($shareInfo[$k]['share_thumb'])){
                unset($shareInfo[$k]);
                continue;
            }
            $shareInfo[$k]['share_copy']=(($shareInfo[$k]['share_text']));
            $shareInfo[$k]['share_text']=nl2br(($shareInfo[$k]['share_text']));

            $shareInfo[$k]['share_data']=htmlspecialchars_decode($shareInfo[$k]['share_data']);
            $shareInfo[$k]['create_time']=date('Y-m-d',strtotime($v['create_time']));
            $shareInfo[$k]['share_pic']=C('UPYUN_DOMAIN').$shareInfo[$k]['share_pic'];//$goodsList[$k]["grade{$this->member['grade_id']}_price"];
            $thumb=json_decode($v['share_thumb'],true);//$goodsList[$k]["grade{$this->member['grade_id']}_price"];
            foreach ($thumb as $sk=>$sv){
                if(empty($sv)){
                    unset($thumb[$sk]);
                    continue;
                }
                $thumb[$sk]=C('UPYUN_DOMAIN').$sv;
            }
            $shareInfo[$k]['share_thumb']=$thumb;
        }
        $this->ajax(200,'success',$shareInfo);
    }


    //添加收藏
    public function addCollectionsList(){
        $add['uid']=$this->uid;
        $add['share_id']=I('post.share_id');
        $add['create_time']=date('Y-m-d H:i:s');
        $re=CollectionsService::S()->add($add);
        if($re){
            $this->ajax(200,'添加成功',$re,'collections_id');
        }else{
            $this->ajax(202,'添加收藏失败');
        }
    }

    //删除
    public function cancel(){
        $where['uid']=$this->uid;
        $share_id=I('post.share_id');
        $where['share_id']=$share_id;
        $re=CollectionsService::S()->delCollections($where);
        if($re){
            $this->ajax(200,'success');
        }else{
            $this->ajax(202,'error');
        }
    }

}